package jwlee.testjfreechart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategorySeriesLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.Rotation;

public class ChartTest {

	public void testPieChart() {
		// 生成饼图数据集对象,注意是DefaultPieDataset
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("One", new Double(43.2D));
		dataset.setValue("Two", new Double(10D));
		dataset.setValue("Three", new Double(27.5D));
		dataset.setValue("Four", new Double(17.5D));
		// 生成图表主对象JFreeChart
		JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo",
				dataset, true, true, false);
		// 获得图表显示对象
		PiePlot pieplot = (PiePlot) chart.getPlot();
		// 抽离section
		// pieplot.setSectionPaint("One", new Color(160, 160, 255));
		// pieplot.setSectionPaint("Two", new Color(128, 128, 223));
		// pieplot.setSectionPaint("Three", new Color(96, 96, 191));
		// pieplot.setSectionPaint("Four", new Color(64, 64, 159));
		pieplot.setExplodePercent("Two", 0.5D);

		pieplot.setNoDataMessage("No data available"); // 没有数据的时候显示的内容
		pieplot.setLabelFont(new Font("SansSerif", Font.BOLD, 12)); // 设置图表标签字体
		pieplot.setCircular(true);// 是否为圆形，否则可拉伸为椭圆
		pieplot.setLabelGap(0.01D);// 间距

		showFreeChart(chart);
	}

	public void testPieChart3D() {
		// 生成饼图数据集对象,注意是DefaultPieDataset
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("One", new Double(43.2D));
		dataset.setValue("Two", new Double(10D));
		dataset.setValue("Three", new Double(27.5D));
		dataset.setValue("Four", new Double(17.5D));
		// 生成图表主对象JFreeChart
		String chartTitle = "Pie Chart Demo";
		JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, dataset,
				true, true, false);
		// 设置饼图下面的图例的字体样式
		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
		// 设置饼图背景色
		chart.setBackgroundPaint(Color.white);
		// 设置饼图标题属性
		Font font = new Font("宋体", Font.PLAIN, 18);
		TextTitle textTitle = new TextTitle("Pie Chart(3D) Demo", font);
		chart.setTitle(textTitle);
		// 获取图形的画布
		PiePlot3D pieplot = (PiePlot3D) chart.getPlot();
		// 设定链接 ("link.jsp","section"));//section为参数，不填写则默认为category
		pieplot.setURLGenerator(new StandardPieURLGenerator("link.jsp",
				"section"));
		// 设定图片的背景透明度（0-1.0之间）
		pieplot.setBackgroundAlpha(0.8f);
		// 设定图片的前景透明度（0-1.0之间）
		pieplot.setForegroundAlpha(0.6f);
		// 设置方向为顺时针方向
		pieplot.setDirection(Rotation.CLOCKWISE);
		// 指定 section 轮廓线的颜色
		pieplot.setOutlinePaint(Color.BLUE);
		// 抽离第一个 section 出来??
		pieplot.setExplodePercent("One", 0.5D);
		// 指定 section 轮廓线的厚度
		pieplot.setOutlineStroke(new BasicStroke(2));
		// 设置第一个 section 的开始位置，默认是12点钟方向90度
		pieplot.setStartAngle(0);
		// 设置图形背景色
		pieplot.setBackgroundPaint(Color.white);
		// 设置图表标签字体
		pieplot.setLabelFont(new Font("宋体", Font.PLAIN, 12));
		// 没有数据的时候显示的内容
		pieplot.setNoDataMessage("无数据显示");
		// 指定显示的饼图上圆形(false)还椭圆形(true)
		pieplot.setCircular(false);
		// 间距
		pieplot.setLabelGap(0.02D);
		// 图片中显示百分比:自定义方式，{0} 表示选项， {1} 表示数值， {2} 表示所占比例 ,小数点后两位
		pieplot.setToolTipGenerator(new StandardPieToolTipGenerator(
				"{0}: ({1}万, {2})", NumberFormat.getNumberInstance(),
				new DecimalFormat("0.00%")));
		// 设置饼图标签的百分比样式
		pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
				"{0}={1}万({2})", NumberFormat.getNumberInstance(),
				new DecimalFormat("0.00%")));
		// 设置饼图下面的图例的百分比样式
		pieplot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
				"{0}: ({1}万, {2})"));

		showFreeChart(chart);
	}

	public void testBarChart() {
		String str1 = "First";
		String str2 = "Second";
		String str3 = "Third";
		String str4 = "Category 1";
		String str5 = "Category 2";
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(1.0D, str1, str4);
		dataset.addValue(4.0D, str1, str5);
		dataset.addValue(5.0D, str2, str4);
		dataset.addValue(7.0D, str2, str5);
		dataset.addValue(4.0D, str3, str4);
		dataset.addValue(3.0D, str3, str5);

		JFreeChart chart = ChartFactory.createBarChart("Bar Chart Demo",
				"Category", "Value", dataset, PlotOrientation.VERTICAL, true,
				true, false);
		CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
		categoryPlot.setDomainGridlinesVisible(true);
		categoryPlot.setRangeCrosshairVisible(true);
		categoryPlot.setRangeCrosshairPaint(Color.blue);
		NumberAxis localNumberAxis = (NumberAxis) categoryPlot.getRangeAxis();
		localNumberAxis.setStandardTickUnits(NumberAxis
				.createIntegerTickUnits());
		BarRenderer localBarRenderer = (BarRenderer) categoryPlot.getRenderer();
		localBarRenderer.setDrawBarOutline(false);
		GradientPaint localGradientPaint1 = new GradientPaint(0.0F, 0.0F,
				Color.blue, 0.0F, 0.0F, new Color(0, 0, 64));
		GradientPaint localGradientPaint2 = new GradientPaint(0.0F, 0.0F,
				Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
		GradientPaint localGradientPaint3 = new GradientPaint(0.0F, 0.0F,
				Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
		localBarRenderer.setSeriesPaint(0, localGradientPaint1);
		localBarRenderer.setSeriesPaint(1, localGradientPaint2);
		localBarRenderer.setSeriesPaint(2, localGradientPaint3);
		localBarRenderer
				.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator(
						"Tooltip: {0}"));
		CategoryAxis localCategoryAxis = categoryPlot.getDomainAxis();
		localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions
				.createUpRotationLabelPositions(0.5235987755982988D));

		showFreeChart(chart);
	}

	private void showFreeChart(JFreeChart jfreechart) {
		ChartPanel chartPanel = new ChartPanel(jfreechart);
		ApplicationFrame appFrame = new ApplicationFrame(null);
		appFrame.setSize(400, 300);
		appFrame.setContentPane(chartPanel);
		RefineryUtilities.centerFrameOnScreen(appFrame);
		appFrame.setVisible(true);
	}

	public static void main(String[] args) {
		ChartTest ct = new ChartTest();
		// ct.testPieChart();
		// ct.testPieChart3D();
		ct.testBarChart();
	}
}
